package day0122

var m, n int
var strategy = [][2]int{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}
var Grid [][]byte

func numIslands(grid [][]byte) int {
	Grid = grid
	m, n = len(grid), len(grid[0])
	var cnt int
	for i := 0; i < m; i++ {
		for j := 0; j < n; j++ {
			if '1' == Grid[i][j] {
				dfs(i, j)
				cnt++
			}
		}
	}
	return cnt
}

func dfs(i int, j int) {
	if i < 0 || i >= m || j < 0 || j >= n {
		return
	}
	if '1' == Grid[i][j] {
		Grid[i][j] = '0'
		for index := 0; index < len(strategy); index++ {
			dfs(i+strategy[index][0], j+strategy[index][1])
		}
	}
}
